﻿

//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "JsonUtility.h"
#include "GbmpNew.h"
#include "StringUtil.h"
#include "FileUtility.h"
#include "DebugMessage.h"
#include "GbmpFileSystem.h"

#include <fstream>
#include "json/writer.h"
#include "json/reader.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

std::string Sample::JsonUtility::SerializeToRawString(const Json::Value& jsonValue)
{
    // GBMP-40456 在函数jsoncpp.dll@0xaba3处发生崩溃
    // 直接使用栈对象 Json::FastWriter 可能造成栈溢出，修改为堆对象，避免造成栈上面指针大面积损坏
    // Json::FastWriter writer;
    SAFE_DEFINE_OWNER_PTR_RETURN_UNLESS(opWriter, std::string(), Json::FastWriter);
    std::string ret = opWriter->write(jsonValue);
    return ret;
}

std::string Sample::JsonUtility::SerializeToHumanReadingString(const Json::Value& jsonValue)
{
    Json::StyledWriter writer;
    std::string ret = writer.write(jsonValue);
    return ret;
}

Json::Value Sample::JsonUtility::UnserializeFromRawString(const std::string& dataStr)
{
    Json::Value ret;
    Json::Reader reader;
    DBG_WARN_AND_RETURN_UNLESS(reader.parse(dataStr, ret, false), Json::Value(), L"json parse error",L"GDMP",L"2024-03-30");
    return ret;
}

Json::Value Sample::JsonUtility::UnserializeFromFile(const std::wstring& filePath)
{
    Json::Reader reader;
    Json::Value root;
    std::ifstream ifile;
    if (FileSystem::OpenStream(ifile, filePath))
    {
        DBG_WARN_AND_RETURN_UNLESS(reader.parse(ifile, root), Json::Value(), L"json parse error",L"GDMP",L"2024-03-30");
        return root;
    }
    return Json::Value();
}

void Sample::JsonUtility::SerializeToFile(const std::wstring& filePath, Json::Value jsonValue)
{
    std::string grepJsonStr = JsonUtility::SerializeToRawString(jsonValue);
    FileUtility::Save(filePath, grepJsonStr.data(), (int)grepJsonStr.size());
}

std::wstring Sample::JsonUtility::ToWString(const Json::Value& jsonValue)
{
    if (jsonValue.isString())
        return StringUtil::ToWString(jsonValue.asString());

    return L"";
}

Json::Value Sample::JsonUtility::ToJson(const std::wstring& wstr)
{
    std::string tempStr = StringUtil::ToString(wstr);
    return Json::Value(tempStr);
}
